home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / register.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.9 KB  |  291 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.command.register
  5.  
  6. Implements the Distutils 'register' command (register with the repository).
  7. """
  8. __revision__ = '$Id: register.py 67944 2008-12-27 13:28:42Z tarek.ziade $'
  9. import os
  10. import string
  11. import urllib2
  12. import getpass
  13. import urlparse
  14. import StringIO
  15. from distutils.core import PyPIRCCommand
  16. from distutils.errors import *
  17. from distutils import log
  18.  
  19. class register(PyPIRCCommand):
  20.     description = 'register the distribution with the Python package index'
  21.     user_options = PyPIRCCommand.user_options + [
  22.         ('list-classifiers', None, 'list the valid Trove classifiers')]
  23.     boolean_options = PyPIRCCommand.boolean_options + [
  24.         'verify',
  25.         'list-classifiers']
  26.     
  27.     def initialize_options(self):
  28.         PyPIRCCommand.initialize_options(self)
  29.         self.list_classifiers = 0
  30.  
  31.     
  32.     def run(self):
  33.         self.finalize_options()
  34.         self._set_config()
  35.         self.check_metadata()
  36.         if self.dry_run:
  37.             self.verify_metadata()
  38.         elif self.list_classifiers:
  39.             self.classifiers()
  40.         else:
  41.             self.send_metadata()
  42.  
  43.     
  44.     def check_metadata(self):
  45.         '''Ensure that all required elements of meta-data (name, version,
  46.            URL, (author and author_email) or (maintainer and
  47.            maintainer_email)) are supplied by the Distribution object; warn if
  48.            any are missing.
  49.         '''
  50.         metadata = self.distribution.metadata
  51.         missing = []
  52.         for attr in ('name', 'version', 'url'):
  53.             if not hasattr(metadata, attr) and getattr(metadata, attr):
  54.                 missing.append(attr)
  55.                 continue
  56.         
  57.         if missing:
  58.             self.warn('missing required meta-data: ' + string.join(missing, ', '))
  59.         
  60.         if metadata.author:
  61.             if not metadata.author_email:
  62.                 self.warn("missing meta-data: if 'author' supplied, " + "'author_email' must be supplied too")
  63.             
  64.         elif metadata.maintainer:
  65.             if not metadata.maintainer_email:
  66.                 self.warn("missing meta-data: if 'maintainer' supplied, " + "'maintainer_email' must be supplied too")
  67.             
  68.         else:
  69.             self.warn('missing meta-data: either (author and author_email) ' + 'or (maintainer and maintainer_email) ' + 'must be supplied')
  70.  
  71.     
  72.     def _set_config(self):
  73.         ''' Reads the configuration file and set attributes.
  74.         '''
  75.         config = self._read_pypirc()
  76.         if config != { }:
  77.             self.username = config['username']
  78.             self.password = config['password']
  79.             self.repository = config['repository']
  80.             self.realm = config['realm']
  81.             self.has_config = True
  82.         elif self.repository not in ('pypi', self.DEFAULT_REPOSITORY):
  83.             raise ValueError('%s not found in .pypirc' % self.repository)
  84.         
  85.         if self.repository == 'pypi':
  86.             self.repository = self.DEFAULT_REPOSITORY
  87.         
  88.         self.has_config = False
  89.  
  90.     
  91.     def classifiers(self):
  92.         ''' Fetch the list of classifiers from the server.
  93.         '''
  94.         response = urllib2.urlopen(self.repository + '?:action=list_classifiers')
  95.         print response.read()
  96.  
  97.     
  98.     def verify_metadata(self):
  99.         ''' Send the metadata to the package index server to be checked.
  100.         '''
  101.         (code, result) = self.post_to_server(self.build_post_data('verify'))
  102.         print 'Server response (%s): %s' % (code, result)
  103.  
  104.     
  105.     def send_metadata(self):
  106.         """ Send the metadata to the package index server.
  107.  
  108.             Well, do the following:
  109.             1. figure who the user is, and then
  110.             2. send the data as a Basic auth'ed POST.
  111.  
  112.             First we try to read the username/password from $HOME/.pypirc,
  113.             which is a ConfigParser-formatted file with a section
  114.             [distutils] containing username and password entries (both
  115.             in clear text). Eg:
  116.  
  117.                 [distutils]
  118.                 index-servers =
  119.                     pypi
  120.  
  121.                 [pypi]
  122.                 username: fred
  123.                 password: sekrit
  124.  
  125.             Otherwise, to figure who the user is, we offer the user three
  126.             choices:
  127.  
  128.              1. use existing login,
  129.              2. register as a new user, or
  130.              3. set the password to a random string and email the user.
  131.  
  132.         """
  133.         if self.has_config:
  134.             choice = '1'
  135.             username = self.username
  136.             password = self.password
  137.         else:
  138.             choice = 'x'
  139.             username = password = ''
  140.         choices = '1 2 3 4'.split()
  141.         while choice not in choices:
  142.             self.announce('We need to know who you are, so please choose either:\n 1. use your existing login,\n 2. register as a new user,\n 3. have the server generate a new password for you (and email it to you), or\n 4. quit\nYour selection [default 1]: ', log.INFO)
  143.             choice = raw_input()
  144.             if not choice:
  145.                 choice = '1'
  146.                 continue
  147.             if choice not in choices:
  148.                 print 'Please choose one of the four options!'
  149.                 continue
  150.         if choice == '1':
  151.             while not username:
  152.                 username = raw_input('Username: ')
  153.             while not password:
  154.                 password = getpass.getpass('Password: ')
  155.             auth = urllib2.HTTPPasswordMgr()
  156.             host = urlparse.urlparse(self.repository)[1]
  157.             auth.add_password(self.realm, host, username, password)
  158.             (code, result) = self.post_to_server(self.build_post_data('submit'), auth)
  159.             self.announce('Server response (%s): %s' % (code, result), log.INFO)
  160.             if not (self.has_config) and code == 200:
  161.                 self.announce('I can store your PyPI login so future submissions will be faster.', log.INFO)
  162.                 self.announce('(the login will be stored in %s)' % self._get_rc_file(), log.INFO)
  163.                 choice = 'X'
  164.                 while choice.lower() not in 'yn':
  165.                     choice = raw_input('Save your login (y/N)?')
  166.                     if not choice:
  167.                         choice = 'n'
  168.                         continue
  169.                 if choice.lower() == 'y':
  170.                     self._store_pypirc(username, password)
  171.                 
  172.             
  173.         elif choice == '2':
  174.             data = {
  175.                 ':action': 'user' }
  176.             data['name'] = data['password'] = data['email'] = ''
  177.             data['confirm'] = None
  178.             while not data['name']:
  179.                 data['name'] = raw_input('Username: ')
  180.             while data['password'] != data['confirm']:
  181.                 while not data['password']:
  182.                     data['password'] = getpass.getpass('Password: ')
  183.                 while not data['confirm']:
  184.                     data['confirm'] = getpass.getpass(' Confirm: ')
  185.                 if data['password'] != data['confirm']:
  186.                     data['password'] = ''
  187.                     data['confirm'] = None
  188.                     print "Password and confirm don't match!"
  189.                     continue
  190.             while not data['email']:
  191.                 data['email'] = raw_input('   EMail: ')
  192.             (code, result) = self.post_to_server(data)
  193.             if code != 200:
  194.                 print 'Server response (%s): %s' % (code, result)
  195.             else:
  196.                 print 'You will receive an email shortly.'
  197.                 print 'Follow the instructions in it to complete registration.'
  198.         elif choice == '3':
  199.             data = {
  200.                 ':action': 'password_reset' }
  201.             data['email'] = ''
  202.             while not data['email']:
  203.                 data['email'] = raw_input('Your email address: ')
  204.             (code, result) = self.post_to_server(data)
  205.             print 'Server response (%s): %s' % (code, result)
  206.         
  207.  
  208.     
  209.     def build_post_data(self, action):
  210.         meta = self.distribution.metadata
  211.         data = {
  212.             ':action': action,
  213.             'metadata_version': '1.0',
  214.             'name': meta.get_name(),
  215.             'version': meta.get_version(),
  216.             'summary': meta.get_description(),
  217.             'home_page': meta.get_url(),
  218.             'author': meta.get_contact(),
  219.             'author_email': meta.get_contact_email(),
  220.             'license': meta.get_licence(),
  221.             'description': meta.get_long_description(),
  222.             'keywords': meta.get_keywords(),
  223.             'platform': meta.get_platforms(),
  224.             'classifiers': meta.get_classifiers(),
  225.             'download_url': meta.get_download_url(),
  226.             'provides': meta.get_provides(),
  227.             'requires': meta.get_requires(),
  228.             'obsoletes': meta.get_obsoletes() }
  229.         if data['provides'] and data['requires'] or data['obsoletes']:
  230.             data['metadata_version'] = '1.1'
  231.         
  232.         return data
  233.  
  234.     
  235.     def post_to_server(self, data, auth = None):
  236.         ''' Post a query to the server, and return a string response.
  237.         '''
  238.         self.announce('Registering %s to %s' % (data['name'], self.repository), log.INFO)
  239.         boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
  240.         sep_boundary = '\n--' + boundary
  241.         end_boundary = sep_boundary + '--'
  242.         body = StringIO.StringIO()
  243.         for key, value in data.items():
  244.             if type(value) not in (type([]), type(())):
  245.                 value = [
  246.                     value]
  247.             
  248.             for value in value:
  249.                 value = unicode(value).encode('utf-8')
  250.                 body.write(sep_boundary)
  251.                 body.write('\nContent-Disposition: form-data; name="%s"' % key)
  252.                 body.write('\n\n')
  253.                 body.write(value)
  254.                 if value and value[-1] == '\r':
  255.                     body.write('\n')
  256.                     continue
  257.             
  258.         
  259.         body.write(end_boundary)
  260.         body.write('\n')
  261.         body = body.getvalue()
  262.         headers = {
  263.             'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8' % boundary,
  264.             'Content-length': str(len(body)) }
  265.         req = urllib2.Request(self.repository, body, headers)
  266.         opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(password_mgr = auth))
  267.         data = ''
  268.         
  269.         try:
  270.             result = opener.open(req)
  271.         except urllib2.HTTPError:
  272.             e = None
  273.             if self.show_response:
  274.                 data = e.fp.read()
  275.             
  276.             result = (e.code, e.msg)
  277.         except urllib2.URLError:
  278.             e = None
  279.             result = (500, str(e))
  280.  
  281.         if self.show_response:
  282.             data = result.read()
  283.         
  284.         result = (200, 'OK')
  285.         if self.show_response:
  286.             print '-' * 75, data, '-' * 75
  287.         
  288.         return result
  289.  
  290.  
  291.